package org.adream.account.rest.api;

import javax.servlet.http.HttpServletRequest;

import org.adream.account.entity.UserShippingAddressEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.service.UserShippingAddressService;
import org.adream.account.util.Constant;
import org.adream.account.util.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户收货地址 controller
 * @author sl
 *
 */
@Controller
@RequestMapping("/address/")
public class UserShippingAddressApiController {
	
	private final static Logger logger = LoggerFactory.getLogger(UserShippingAddressApiController.class);
	
	@Autowired
	private UserShippingAddressService addressService;
	
	/**
	 * 新增收货地址
	 * @param address
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addAddress", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<?> addAddress(@RequestBody UserShippingAddressEntity address, HttpServletRequest request) {
		if(address == null) {
			logger.warn("新增失败,数据为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "新增失败,参数为空", null);
		}
		address.setUid(!StringUtils.isEmpty(address.getUid()) ? address.getUid() : UserUtil.getUidByRequest(request));
		if(StringUtils.isEmpty(address.getUid())) {
			logger.warn("新增失败,参数uid为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "新增失败,参数为空", null);
		}
		//验证数据长度
		if(!StringUtils.isEmpty(address.getReceiver()) && address.getReceiver().length() > Constant.COMMON_DATA_LENGTH) {
			logger.warn("收货人输入过长,收货人数据长度:" + address.getReceiver().length());
			return new ResultModel<String>(ResultModel.WARN_DATA_LENGTH, "收货人输入过长,请精简", null);
		}
		
		if(!StringUtils.isEmpty(address.getPhone()) && address.getPhone().length() > Constant.ELEVEN) {
			logger.warn("手机号码输入过长,手机号码数据长度:" + address.getPhone().length());
			return new ResultModel<String>(ResultModel.WARN_DATA_LENGTH, "手机号码输入过长,请精简", null);
		}
		
		if(!StringUtils.isEmpty(address.getAddress()) && address.getAddress().length() > Constant.LONG_DATA_LENGTH) {
			logger.warn("详细地址输入过长,详细地址数据长度:" + address.getAddress().length());
			return new ResultModel<String>(ResultModel.WARN_DATA_LENGTH, "详细地址输入过长,请精简", null);
		}
		return addressService.addAddress(address);
	}
	
	/**
	 * 删除收货地址
	 * @param uid	user id
	 * @param sort	排序值
	 * @return
	 */
	@RequestMapping(value = "deleteAddress", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<?> deleteAddress(String uid,String sort) {
		if(StringUtils.isEmpty(uid) || StringUtils.isEmpty(sort)) {
			logger.warn("删除失败,参数为空:(");			
			return new ResultModel<String>(ResultModel.ERR_PARAM, "新增失败,参数为空", null);
		}
		return addressService.deleteAddress(uid,sort);
	}
	
	/**
	 * 更新收货地址
	 * @param address
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updateAddress", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<?> updateAddress(@RequestBody UserShippingAddressEntity address, HttpServletRequest request) {
		if(address == null) {
			logger.warn("更新失败,数据为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "更新失败,参数为空", null);
		}
		address.setUid(!StringUtils.isEmpty(address.getUid()) ? address.getUid() : UserUtil.getUidByRequest(request));
		if(StringUtils.isEmpty(address.getUid()) || StringUtils.isEmpty(address.getSort())) {
			logger.warn("更新失败,参数uid或sort为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "更新失败,参数为空", null);
		}
		
		//验证数据长度
		if(!StringUtils.isEmpty(address.getReceiver()) && address.getReceiver().length() > Constant.COMMON_DATA_LENGTH) {
			logger.warn("收货人输入过长,收货人数据长度:" + address.getReceiver().length());
			return new ResultModel<String>(ResultModel.WARN_DATA_LENGTH, "收货人输入过长,请精简", null);
		}
		
		if(!StringUtils.isEmpty(address.getPhone()) && address.getPhone().length() > Constant.ELEVEN) {
			logger.warn("手机号码输入过长,手机号码数据长度:" + address.getPhone().length());
			return new ResultModel<String>(ResultModel.WARN_DATA_LENGTH, "手机号码输入过长,请精简", null);
		}
		
		if(!StringUtils.isEmpty(address.getAddress()) && address.getAddress().length() > Constant.LONG_DATA_LENGTH) {
			logger.warn("详细地址输入过长,详细地址数据长度:" + address.getAddress().length());
			return new ResultModel<String>(ResultModel.WARN_DATA_LENGTH, "详细地址输入过长,请精简", null);
		}
		return addressService.updateAddress(address);
	}
	
	/**
	 * 设置默认收货地址
	 * @param uid	user id
	 * @param sort	排序
	 * @return
	 */
	@RequestMapping(value = "setDefaultAddress", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultModel<?> setDefaultAddress(String uid, String sort) {
		if (StringUtils.isEmpty(uid) || StringUtils.isEmpty(sort)) {
			logger.warn("设置用户收货地址失败,数据为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "设置失败,参数为空", null);
		}
		return addressService.setDefaultAddress(uid, sort);
	}
	
	/**
	 * 根据删除标志查询收货地址
	 * @param dr	删除标志
	 * @return
	 */
	@RequestMapping(value = "queryAddressesByDr", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> queryAddressesByDr(Integer dr) {
		if(dr == null) {
			logger.warn("根据删除标志查询收货地址");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "设置失败,参数为空", null);			
		}
		return addressService.queryAddressesByDr(dr);
	}
	
	/**
	 * 根据uid和删除标志查询收货地址
	 * @param uid	user id
	 * @param dr	删除标志
	 * @return
	 */
	@RequestMapping(value = "queryAddressesByUidAndDr", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> queryAddressByUidAndDr(String uid, Integer dr) {
		if(StringUtils.isEmpty(uid) || dr == null) {
			logger.warn("根据uid和删除标志查询收货地址");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "设置失败,参数为空", null);						
		}
		return addressService.queryAddressesByUidAndDr(uid, dr);
	}
	
	/**
	 * 根据uid,排序,删除标志查询收货地址 
	 * @param uid	user id
	 * @param sort	排序值
	 * @param dr	删除标志
	 * @return
	 */
	@RequestMapping(value = "queryAddressByPkAndDr", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> queryAddressByPkAndDr(String uid,String sort,Integer dr) {
		if(StringUtils.isEmpty(uid) || StringUtils.isEmpty(sort) || dr == null) {
			logger.warn("根据uid,排序值,删除标志查询收货地址");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "设置失败,参数为空", null);						
		}
		return addressService.queryAddressByPkAndDr(uid, sort, dr);
	}
	
	/**
	 * 查询当前用户的收货地址
	 * @param dr
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "querySelfAddresses", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> querySelfAddresses(Integer dr,HttpServletRequest request) {
		if(dr == null) {
			logger.warn("查询当前用户的收货地址失败,删除标志为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "查询失败,参数为空", null);
		}
		String uid = UserUtil.getUidByRequest(request);
		if(StringUtils.isEmpty(uid)) {
			logger.warn("查询当前用户的收货地址失败,uid为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "查询失败,参数为空", null);
		}
		return addressService.queryAddressesByUidAndDr(uid, dr);
	}
	
	/**
	 * 查询当前用户特定排序值的收货地址
	 * @param sort	排序值
	 * @param dr	删除标志
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "querySelfAddressByPkAndDr", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> querySelfAddressByPkAndDr(String sort,Integer dr, HttpServletRequest request) {
		if(StringUtils.isEmpty(sort) || dr == null) {
			logger.warn("查询当前用户特定排序值的收货地址失败,排序值或删除标志为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "查询失败,参数为空", null);
		}
		String uid = UserUtil.getUidByRequest(request);
		if(StringUtils.isEmpty(uid)) {
			logger.warn("查询当前用户特定排序值的收货地址失败,uid为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "查询失败,参数为空", null);
		}
		return addressService.queryAddressByPkAndDr(uid, sort, dr);
	}
	
	/**
	 * 统计当前用户收货地址 
	 * @param dr	删除标志
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "countSelfAddress", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> countSelfAddress(Integer dr, HttpServletRequest request) {
		String uid = UserUtil.getUidByRequest(request);
		if(StringUtils.isEmpty(uid) || dr == null) {
			logger.warn("统计当前用户收货地址失败,请求参数为空");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "统计失败,参数为空", null);
		}
		return addressService.countAddressesByUidAndDr(uid, dr);
	}
	
	/**
	 * 新增/更新用户收货地址
	 * @param address	收货地址
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "saveOrUpdateAddress", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<?> saveOrUpdateAddress(@RequestBody UserShippingAddressEntity address, HttpServletRequest request) {
		if(address == null) {
			logger.warn("保存或更新收货地址失败,数据为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "操作失败,参数为空", null);
		}
		address.setUid(UserUtil.getUidByRequest(request));
		if(StringUtils.isEmpty(address.getUid())) {
			logger.warn("保存或更新收货地址失败,参数uid为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "操作失败,参数为空", null);
		}
		//验证数据长度
		if(!StringUtils.isEmpty(address.getReceiver()) && address.getReceiver().length() > Constant.COMMON_DATA_LENGTH) {
			logger.warn("收货人输入过长,收货人数据长度:" + address.getReceiver().length());
			return new ResultModel<String>(ResultModel.WARN_DATA_LENGTH, "收货人输入过长,请精简", null);
		}
		
		if(!StringUtils.isEmpty(address.getPhone()) && address.getPhone().length() > Constant.ELEVEN) {
			logger.warn("手机号码输入过长,手机号码数据长度:" + address.getPhone().length());
			return new ResultModel<String>(ResultModel.WARN_DATA_LENGTH, "手机号码输入过长,请精简", null);
		}
		
		if(!StringUtils.isEmpty(address.getAddress()) && address.getAddress().length() > Constant.LONG_DATA_LENGTH) {
			logger.warn("详细收货地址输入过长,详细收货地址数据长度:" + address.getAddress().length());
			return new ResultModel<String>(ResultModel.WARN_DATA_LENGTH, "详细收货地址输入过长,请精简", null);
		}
		return addressService.saveOrUpdateAddress(address);
	}
	
	/**
	 * 删除当前用户特定的排序值收货地址失败
	 * @param sort	排序值
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "deleteSelfAddress", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> deleteSelfAddress(String sort, HttpServletRequest request) {
		if(StringUtils.isEmpty(sort)) {
			logger.warn("删除当前用户特定的排序值收货地址失败,排序值为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "删除失败,参数为空", null);
		}
		String uid = UserUtil.getUidByRequest(request);
		if(StringUtils.isEmpty(uid)) {
			logger.warn("删除当前用户特定的排序值收货地址失败,uid为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "删除失败,参数为空", null);
		}
		return addressService.deleteAddress(uid, sort);
	}
	
	/**
	 * 设置当前用户特定收货地址为默认收获地址
	 * @param sort	排序值
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "setSelfDefaultAddress", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> setSelfDefaultAddress(String sort, HttpServletRequest request) {
		if(StringUtils.isEmpty(sort)) {
			logger.warn("设置当前用户特定收货地址为默认收货地址失败,排序值为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "设置失败,参数为空", null);
		}
		String uid = UserUtil.getUidByRequest(request);
		if (StringUtils.isEmpty(uid)) {
			logger.warn("设置当前用户特定收货地址为默认收货地址失败,uid为空:(");
			return new ResultModel<String>(ResultModel.ERR_PARAM, "参数失败,参数为空", null);
		}
		return addressService.setDefaultAddress(uid, sort);
	}
}